<template>
	<view class="content" @click="imagesa">
		<image src="../../static/images/index/微信图片_20240523144748.jpg" style="width: 100vw;height: 100vh;"></image>

	</view>
</template>

<script>
	import request from '../../api/index.js'
	import api from '../../api/Channel.js'
	import commonMixin from '@/mixins/qiLiuyun.js';
	export default {
		mixins: [commonMixin],
		data() {
			return {

			}
		},
		onShareAppMessage() {
			return {
				title: '给你推荐一个好用的商城系统',
				path: '/pages/index/index?referrer=123', // 打开时可以拿到参数
				imageUrl: '../../static/images/index/微信图片_20240523144748.jpg'
			};
		},
		onLoad() {
			// #ifdef MP-XHS ||  MP-TOUTIAO
			this.loginKey()
			// #endif

		},
		methods: {
			doyces() {

			},
			async loginKey() {
				// grant_type: 'client_credential', // 固定值
				// client_key: 'ttb433c28e2b3d249101', // 替换为你的 AppID
				// client_secret: '5e29822bd7a2c1fcac0f20cefab49aa21f9534b7' // 替换为你的 AppSecret
				// uni.setStorageSync('token', res.data.token)
				let obj = {
					data: {
						"code": "1",
						"appId": "1"
					},
					url: api.ctrl + '/users/login/usingXhs',
					method: "POST",
				}
				let res = await request(obj)
				uni.setStorageSync('token', res.data.token)
			},
			imagesa() {
				const value = uni.getStorageSync('token') || null
				console.log(value)
				if (value) {

				} else {
					uni.showToast({
						title: '请先登陆',
						icon: 'none'
					})
					return
				}
				let th = this
				
				uni.chooseImage({
					count: 1,
					sourceType: ['camera', 'album'],
					crop: {
						width: 300,
						height: 300
					}, // 1:1 比例裁剪,
					success(res) {
						// #ifdef MP-TOUTIAO
						// th.imageToBase64(res.tempFilePaths[0], value)
						
						// uni.navigateTo({
						// 	url: '/pages/eachjie/eachjie?url=' + JSON.stringify(res.tempFilePaths[0])
						// })
						uni.navigateTo({
							url: '/pages/eachjie/eachjie?url=' + JSON.stringify(res.tempFilePaths[0])
						})
						// #endif

						// #ifndef  MP-TOUTIAO
						uni.navigateTo({
							url: '/pages/eachjie/eachjie?url=' + JSON.stringify(res.tempFilePaths[0])
						})
						// #endif
					},
					fail(res) {
						console.log(res)
						if (res.errNo == 10200) {
							th.pandxj()
						}
					}
				})
			},
			imageToBase64(tempFilePath, value) {
				const fs = uni.getFileSystemManager(); // 获取文件系统管理器
				fs.readFile({
					filePath: tempFilePath,
					encoding: 'base64', // 指定编码为 Base64
					success: (res) => {
						const base64Data = `data:image/jpeg;base64,${res.data}`; // 补全格式
						console.log(value)
						uni.request({
							url: 'https://developer.toutiao.com/api/apps/censor/image',
							method: "POST",
							data: {
								app_id: 'ttb433c28e2b3d249101', // 必填，替换为你的小程序ID
								access_token: value, // 必填，替换为你的access_token
								image_data: base64Data // 选填，要检测的图片URL
							},
							success(res) {
								console.log(res)
							}
						})
						// 可以存储或上传
					},
					fail: (err) => {
						console.error("转换失败:", err);
					}
				});
			},



			pandxj() {
				uni.getSetting({
					success(res) {
						if (!res.authSetting['camera']) {
							console.log('相机权限未开启');
							// 尝试拉起权限
							uni.showModal({
								title: '提示',
								content: '是否要开启相机权限？',
								success(res) {
									if (res.confirm) {
										// 点击确定按钮，尝试拉起权限
										uni.openSetting({
											success(res) {
												console.log(res.authSetting);
											},
										});
									}
								},
							});
						} else {
							console.log('相机权限已开启');
						}
					},
				});

			},
		}
	}
</script>

<style>
	.content {
		width: 100vw;
		height: 100vh;
	}
</style>